#!/bin/bash

# 更新系统包
echo "Updating system packages..."
sudo yum update -y
yum install -y wget

# 安装OpenJDK 1.8
echo "Installing OpenJDK 1.8..."
sudo yum install java-1.8.0-openjdk-devel -y

# 验证Java安装
echo "Verifying Java installation..."
java -version
if [ $? -ne 0 ]; then
    echo "Java installation failed."
    exit 1
fi

# 配置环境变量
echo "Configuring environment variables for Java..."
JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
JRE_HOME=$JAVA_HOME/jre
echo "export JAVA_HOME=$JAVA_HOME" | sudo tee -a /etc/profile
echo "export JRE_HOME=$JRE_HOME" | sudo tee -a /etc/profile
echo "export PATH=\$PATH:\$JAVA_HOME/bin:\$JRE_HOME/bin" | sudo tee -a /etc/profile
source /etc/profile

# 下载并解压Maven
MAVEN_VERSION=3.8.8
MAVEN_TAR_FILE=apache-maven-$MAVEN_VERSION-bin.tar.gz
MAVEN_URL=https://dlcdn.apache.org/maven/maven-3/$MAVEN_VERSION/binaries/$MAVEN_TAR_FILE

echo "Downloading Maven..."
wget $MAVEN_URL -O /tmp/$MAVEN_TAR_FILE

echo "Extracting Maven..."
sudo tar -zxvf /tmp/$MAVEN_TAR_FILE -C /opt/
rm -rf /opt/maven
sudo ln -s /opt/apache-maven-$MAVEN_VERSION /opt/maven

# 配置环境变量
echo "Configuring environment variables for Maven..."
echo "export MAVEN_HOME=/opt/maven" | sudo tee -a /etc/profile
echo "export PATH=\$PATH:\$MAVEN_HOME/bin" | sudo tee -a /etc/profile
source /etc/profile

# 验证Maven安装
echo "Verifying Maven installation..."
mvn -version
if [ $? -ne 0 ]; then
    echo "Maven installation failed."
    exit 1
fi

echo "JDK 1.8 and Maven installation completed successfully."

mv /opt/apache-maven-3.8.8/conf/settings.xml /opt/apache-maven-3.8.8/conf/settings.xml.$(date +%s)
cp /app/pro/cz-23yw/settings.xml /opt/apache-maven-3.8.8/conf/settings.xml